void decoder::save_result(int num_frames)
{
    HistoryVector path;
    toolbox_.tp_search().get_path(path, true, NULL);

    ostringstream oss;
    bool word_boundary = false;
    bool empty = true;

    HistoryVector::const_reverse_iterator iter = path.rbegin();
    for (; iter != path.rend(); ++iter) {
        TPLexPrefixTree::LMHistory * history = *iter;
        BOOST_ASSERT(history->reference_count > 0);

        const string & word = toolbox_.word(history->word_id);
        if (word == "<s>") {
            continue;
        }
        if (word == "</s>") {
            continue;
        }
        if (word == "<w>") {
            // Word boundary symbol. If the next word is not the first one, add
            // a space in front of it.
            if (!empty)
                word_boundary = true;
            continue;
        }
        if (lm_->is_word_model()) {
            // Word language model. Add a space in front of all the words but
            // the first one.
            if (!empty)
                word_boundary = true;
        }
        if (word_boundary) {
            oss << " ";
            word_boundary = false;
        }
        oss << word;
        empty = false;
    }

    result_ = oss.str();

    if (num_frames > 0) {
        float am_log_prob = toolbox_.tp_search().get_am_log_prob(true);
        am_log_prob *= log_prob_scale_factor_;
        confidence_ = exp(am_log_prob / num_frames);
    }
    else {
        confidence_ = 0;
    }
} 
